/*
 * @Author: 王炜皓
 * @Date: 2023-06-15 14:54:10
 * @LastEditTime: 2023-06-15 19:38:51
 * @LastEditors: 王炜皓
 * @Description: 多边形绘制
 * @FilePath: \cesium-tool\src\views\test\extend\Draw\Polygon\Polygon.ts
 */


  import CreateRegularPolygon from './CreatePolygon'
import { newSessionid, getCatesian3FromPX ,createRemindertip,destroyTip} from "../../plotCommon";

  import EditPolygon from './EditPolygon'
  const Cesium = window.Cesium;
  export default class Polygon{
    handler:any;
    _viewer:any
    _options:any
    editPolygonClass:any
    pickerEntity:any
    _entities:Array<any>
    constructor(viewer:any){
      this._viewer=viewer
      this._entities=[]
    }
    get options(){
      return this._options
    }
    set options(val){
      val=Object.assign({},val)
      this._options=val
    }
    get entities() {
      return this._entities;
    }
    set entities(val) {
      this._entities = val;
    }
    create(){
     let viewer=this._viewer
      if (this.handler) this.handler.destroy();
      this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
      const resultList:any = [];
      const callback = (event:any) => {
        console.log(event, "event");
        this._entities.push(event)
        // if (callbackForInfo && typeof callbackForInfo == "function")
          // callbackForInfo(event);
      };
      CreateRegularPolygon(viewer, this.handler,resultList, this._options, callback);
    }
    stopCreate(){
      this.handler.destroy()
    }
    edit(editCalback:any){
     let viewer=this._viewer

      this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
      const calback = (e:any) => {
        if (editCalback && typeof editCalback == "function")editCalback(e)
        if (this.editPolygonClass) this.editPolygonClass.clear();
        this.getPicking(this.handler, viewer, calback, false);
        // EditBillboard(e,this.handler,[])
        // this.editPolyLine = new EditPolyline(e,this.handler,[]);
        this.editPolygonClass = new EditPolygon(e, viewer, this.handler);
        this.editPolygonClass.startEdit();
        // this.editPolyLine.editTranslation();
      };
      const openTripParemeter = "code";
      this.getPicking(this.handler, viewer, calback, false);
    }
    stopEdit(){
      if (this.handler) this.handler.destroy();
      if(this.editPolygonClass)this.editPolygonClass.stopEdit();
    }
    removeById(id:any){
      this._viewer.entities.removeById(id);
      this._entities = this._entities.filter((item) => item.id != id);
    }
    getPicking(handler:any, viewer:any, callback:any, openTripParemeter:any) {
      // 创建一个ScreenSpaceEventHandler对象
      let _this = this;
      handler.setInputAction(function (click:any) {
        console.log("getPicking正在拾取坐标");
  
        var position = click.position;
        var pickedObject = viewer.scene.pick(position);
        // 检查pickedObject是否为实体
        if (
          Cesium.defined(pickedObject) &&
          Cesium.defined(pickedObject.id) &&
          pickedObject.id instanceof Cesium.Entity
        ) {
          // 获取点击的实体+
          var entity = pickedObject.id;
          // 输出实体信息
          const toolTip = pickedObject.id;
          _this.pickerEntity = entity;
          if (callback && typeof callback == "function") callback(entity);
          if (openTripParemeter && typeof openTripParemeter == "string")
            createRemindertip(
              entity[openTripParemeter],
              click.position,
              true
            );
        }
      }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    }
  }